package com.softtown.modules.sys.controller;

import com.github.pagehelper.PageInfo;
import com.qcloud.cos.utils.StringUtils;
import com.softtown.common.annotation.LogOperation;
import com.softtown.common.exception.ErrorCode;
import com.softtown.common.regularUtils.RegularUtils;
import com.softtown.common.utils.Result;
import com.softtown.modules.sys.dto.CompSysUserApplPageDTO;
import com.softtown.modules.sys.dto.JoinCompanyDTO;
import com.softtown.modules.sys.entity.CompSysUserApplEntity;
import com.softtown.modules.sys.entity.CompSysUserEmplEntity;
import com.softtown.modules.sys.entity.SysCompEntity;
import com.softtown.modules.sys.service.CompSysUserApplService;
import com.softtown.modules.sys.service.CompSysUserEmplService;
import com.softtown.modules.sys.service.SysCompService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.*;

/**
 * @author: Dingyayong
 * @Date: 2019/6/3 17:18
 * @Description:加入公司
 */
@RestController
@RequestMapping("/sys/join")
@Api(tags = "加入公司")
public class CompSysUserApplController {

    @Resource
    private SysCompService sysCompService;

    @Resource
    private CompSysUserApplService compSysUserApplService;

    @Resource
    private CompSysUserEmplService compSysUserEmplService;

    @PostMapping("getUserApplList")
    @ApiOperation("获取申请分页")
    @LogOperation("获取申请分页")
    public Result getUserApplList(@RequestBody CompSysUserApplPageDTO compSysUserApplPageDTO) {
        PageInfo<CompSysUserApplEntity> page = compSysUserApplService.getUserApplList(compSysUserApplPageDTO);
        return new Result<PageInfo<CompSysUserApplEntity>>().ok(page);
    }

    @PostMapping("joinCompany")
    @ApiOperation("申请加入公司")
    @LogOperation("申请加入公司")
    public Result joinCompany(@RequestBody JoinCompanyDTO joinCompanyDTO) {
        Result result = new Result<>();
        if (StringUtils.isNullOrEmpty(joinCompanyDTO.getCompanyName()) && StringUtils.isNullOrEmpty(joinCompanyDTO.getUscc())) {
            return result.error(ErrorCode.COMPANY_NAME_USCC);
        }
        if (StringUtils.isNullOrEmpty(joinCompanyDTO.getCompanyName()) && !StringUtils.isNullOrEmpty(joinCompanyDTO.getUscc())) {
            if (!RegularUtils.isuscc(joinCompanyDTO.getUscc())) {
                return result.error(ErrorCode.INCORRECT_USCC_FORMAT);
            } else {
                SysCompEntity sysCompEntity = sysCompService.getCompanyByUscc(joinCompanyDTO.getUscc());
                if (sysCompEntity == null) {
                    return result.error(ErrorCode.COMPANY_NOT_EXISTS);
                } else {
                    CompSysUserEmplEntity compSysUserEmplEntity = compSysUserEmplService.findCompEmpl(sysCompEntity.getId(), joinCompanyDTO.getUserId());
                    if (compSysUserEmplEntity == null) {
                        CompSysUserApplEntity compSysUserApplEntity = compSysUserApplService.findByUserIdAndCompId(sysCompEntity.getId(), joinCompanyDTO.getUserId());
                        if (compSysUserApplEntity != null) {
                            return result.error(ErrorCode.USERAPPLCOMP_ERROR);
                        } else {
                            compSysUserApplService.joinCompany(sysCompEntity, joinCompanyDTO);
                        }
                    } else {
                        return result.error(ErrorCode.COMPANY_EMPL_EXISTS);
                    }

                }
            }
        } else if (StringUtils.isNullOrEmpty(joinCompanyDTO.getUscc()) && !StringUtils.isNullOrEmpty(joinCompanyDTO.getCompanyName())) {
            if (RegularUtils.COMPANY_NAME.matches(joinCompanyDTO.getCompanyName())) {
                return result.error(ErrorCode.COMPANY_NAME_TYPE);
            } else {
                SysCompEntity sysCompEntity = sysCompService.getCompanyByCampanyName(joinCompanyDTO.getCompanyName());
                if (sysCompEntity == null) {
                    return result.error(ErrorCode.COMPANY_NOT_EXISTS);
                } else {
                    CompSysUserEmplEntity compSysUserEmplEntity = compSysUserEmplService.findCompEmpl(sysCompEntity.getId(), joinCompanyDTO.getUserId());
                    if (compSysUserEmplEntity != null) {
                        return result.error(ErrorCode.COMPANY_EMPL_EXISTS);
                    } else {
                        CompSysUserApplEntity compSysUserApplEntity = compSysUserApplService.findByUserIdAndCompId(sysCompEntity.getId(), joinCompanyDTO.getUserId());
                        if (compSysUserApplEntity != null) {
                            return result.error(ErrorCode.USERAPPLCOMP_ERROR);
                        } else {
                            compSysUserApplService.joinCompany(sysCompEntity, joinCompanyDTO);
                        }
                    }
                }
            }
        } else {
            if (!RegularUtils.COMPANY_NAME.matches(joinCompanyDTO.getCompanyName())) {
                SysCompEntity sysCompEntity = sysCompService.getCompanyByCampanyName(joinCompanyDTO.getCompanyName());
                if (sysCompEntity == null) {
                    return result.error(ErrorCode.COMPANY_NOT_EXISTS);
                } else {
                    CompSysUserApplEntity compSysUserApplEntity = compSysUserApplService.findByUserIdAndCompId(sysCompEntity.getId(), joinCompanyDTO.getUserId());
                    if (compSysUserApplEntity != null) {
                        return result.error(ErrorCode.USERAPPLCOMP_ERROR);
                    } else {
                        compSysUserApplService.joinCompany(sysCompEntity, joinCompanyDTO);
                    }
                }
            } else {
                return result.error(ErrorCode.COMPANY_NAME_TYPE);
            }
        }
        return result;
    }

    @PostMapping("checkCompany")
    @ApiOperation("检查公司是否存在")
    @LogOperation("检查公司是否存在")
    public Result checkCompany(@RequestBody JoinCompanyDTO joinCompanyDTO) {
        Result result = new Result<>();
        if (StringUtils.isNullOrEmpty(joinCompanyDTO.getCompanyName()) && StringUtils.isNullOrEmpty(joinCompanyDTO.getUscc())) {
            return result.error(ErrorCode.COMPANY_NAME_USCC);
        }
        if (StringUtils.isNullOrEmpty(joinCompanyDTO.getCompanyName()) && !StringUtils.isNullOrEmpty(joinCompanyDTO.getUscc())) {
            if (RegularUtils.isuscc(joinCompanyDTO.getUscc())) {
                SysCompEntity sysCompEntity = sysCompService.getCompanyByUscc(joinCompanyDTO.getUscc());
                if (sysCompEntity == null) {
                    return result.error(ErrorCode.COMPANY_NOT_EXISTS);
                } else {
                    return new Result<>().ok(sysCompEntity.getName());
                }
            } else {
                return result.error(ErrorCode.INCORRECT_USCC_FORMAT);
            }
        } else if (StringUtils.isNullOrEmpty(joinCompanyDTO.getUscc()) && !StringUtils.isNullOrEmpty(joinCompanyDTO.getCompanyName())) {
            if (!RegularUtils.COMPANY_NAME.matches(joinCompanyDTO.getCompanyName())) {
                SysCompEntity sysCompEntity = sysCompService.getCompanyByCampanyName(joinCompanyDTO.getCompanyName());
                if (sysCompEntity == null) {
                    return result.error(ErrorCode.COMPANY_NOT_EXISTS);
                } else {
                    return new Result<>().ok(sysCompEntity.getName());
                }
            } else {
                return result.error(ErrorCode.COMPANY_NAME_TYPE);
            }
        } else {
            if (RegularUtils.COMPANY_NAME.matches(joinCompanyDTO.getCompanyName())) {
                return result.error(ErrorCode.COMPANY_NAME_TYPE);
            } else {
                SysCompEntity sysCompEntity = sysCompService.getCompanyByCampanyName(joinCompanyDTO.getCompanyName());
                if (sysCompEntity == null) {
                    return result.error(ErrorCode.COMPANY_NOT_EXISTS);
                } else {
                    return new Result<>().ok(sysCompEntity.getName());
                }
            }
        }
    }
}
